package cn.livingCloud.controller.core;

import cn.livingCloud.dto.CaseCommentDto;
import cn.livingCloud.dto.CaseCommentFormDto;
import cn.livingCloud.dto.CaseCommentListDto;
import cn.livingCloud.dto.JsonDto;
import cn.livingCloud.service.CaseService;
import cn.livingCloud.utils.BindingResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;

/**
 * 案件控制器
 * Created by DingJie on 2016/6/2.
 */
@Controller
@RequestMapping("/admin/cmc/case")
public class CaseCommentsController {
    @Autowired
    private CaseService service;


    //留言列表
    @RequestMapping("/comments.htm")
    public String comments(@RequestParam("uuid") String uuid, Model model) {
        CaseCommentListDto listDto = service.loadComments(uuid);
        model.addAttribute("listDto", listDto);
        return "cmc/case/comments";
    }

    //添加留言
    @RequestMapping(value = "/add_comments", method = RequestMethod.POST)
    @ResponseBody
    public JsonDto submitComments(@Valid CaseCommentFormDto formDto, BindingResult result, Model model) {
        if (result.hasErrors()) {
            return BindingResultUtils.buildErrorMessage(result, new JsonDto());
        }
        return service.submitComments(formDto);
    }

    //屏蔽
    @RequestMapping("/disable")
    public String disable(@RequestParam("uuid") String uuid, Model model) {
        CaseCommentDto caseCommentDto = service.disableComments(uuid);
        model.addAttribute("item", caseCommentDto);
        return "cmc/case/comments_item";
    }

}
